<?php
App::uses('AppController', 'Controller');



class NluploadsController extends AppController 
{
    

/**
 *  view method
 *
 * @return void
 */
   // var $name = 'Nlupload';
      
        public function index() {
                $this->set('Nluploads', $this->Nlupload->find('all'));
		$this->Nlupload->recursive = 1;
		$this->set('uploads', $this->paginate());
	}
    
	public function view() {
                $this->set('Nluploads', $this->Nlupload->find('all'));
		$this->Nlupload->recursive = 1;
		$this->set('uploads', $this->paginate());
	}

	/**
	 * add for specific user method
	 *
	 * @return void
	 */


		public function add() {
			if ($this->request->is('post')) {
				$this->Nlupload->create();
	            //upload image if user has selected one
	            $fileOK = $this->uploadFiles('files/newsletters', $this->data['Nlupload']['FILENAME']);

	            if(array_key_exists('urls', $fileOK))
	            {
	                $this->request->data['Nlupload']['FILENAME'] = $fileOK['urls'][0];

	                // set date field to current time
	                $this->request->data['Nlupload']['UPLOADDATE'] = date('Y-m-d H:i:s'); // MIGHT NEED TO CHANGE!

	                if ($this->Nlupload->save($this->request->data))
	                {
	                    $this->Session->setFlash('The document has been saved','default',array('class'=>'alert alert-info'));
	                    //  REDIRECT TO HOME?? $this->redirect(array('controller' => 'Patients', 'action' => 'view', $id));
	                    $this->redirect(array('action' => 'view'));
	                } else
	                {
	                    $this->Session->setFlash('The new document could not be saved. Please, try again.','default',array('class'=>'alert alert-danger'));
	                }
	            }
	            else
	            {
	             $this->Session->setFlash('The new document could not be saved. Please make sure you are uploading a PDF.','default',array('class'=>'alert alert-danger'));
	            }
			}
	    }
            public function delete($id = null) {
		$this->Nlupload->id = $id;
		if (!$this->Nlupload->exists()) {
			throw new NotFoundException('Invalid newsletter','default',array('class'=>'alert alert-danger'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Nlupload->delete()) {
			$this->Session->setFlash('Newsletter deleted','default',array('class'=>'alert alert-info'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash('Newsletter was not deleted','default',array('class'=>'alert alert-danger'));
		$this->redirect(array('action' => 'index'));
	}

	        public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('view','delete');
    }
}

